package com.stu.stusystem;

import com.stu.stusystem.controller.netty.NettyServer;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync;
import tk.mybatis.spring.annotation.MapperScan;

import java.net.InetAddress;
import java.net.UnknownHostException;

@SpringBootApplication
@MapperScan(basePackages = "com.stu.stusystem.mapper")
@Slf4j
@EnableAsync
public class StusystemApplication {
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(StusystemApplication.class);
        Environment env = app.run(args).getEnvironment();
        logApplicationStartup(env);

        // 启动netty
//        new NettyServer().start(2019);
    }

    private static void logApplicationStartup(Environment env) {
        String protocol = "http";
        if (env.getProperty("server.ssl.key-store") != null) {
            protocol = "https";
        }
        String serverPort = env.getProperty("server.port");
        String contextPath = env.getProperty("server.servlet.context-path");
        if (StringUtils.isBlank(contextPath)) {
            contextPath = "/";
        }
        String hostAddress = "localhost";
        try {
            hostAddress = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            log.warn("The host name could not be determined, using `localhost` as fallback");
        }
        log.info("\n----------------------------------------------------------\n\t" +
                        " 　☆ *　. 　☆\n" +
                        "　　.∧＿∧＿n　* ☆\n" +
                        "* ☆ ( ・∀・)つ .\n" +
                        "　. ⊂　　 ノ* ☆\n" +
                        "☆ * (つ ノ .☆\n" +
                        "　　 (ノ\n\t" +
                        "stuSystem系统启动! Access URLs:\n\t" +
                        "Local: \t\t{}://localhost:{}{}\n\t" +
                        "External: \t{}://{}:{}{}\n\t" +
                        "Profile(s): \t{}\n----------------------------------------------------------",
                protocol,
                serverPort,
                contextPath,
                protocol,
                hostAddress,
                serverPort,
                contextPath,
                env.getActiveProfiles());
    }
}
